﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class ChiTietDienThoaiDAO
    {
        #region 1   -   Queries

        public static ChiTietDienThoaiDTO LayChiTietDienThoaiTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            CHI_TIET_DIEN_THOAI chiTietDienThoai = myDB.CHI_TIET_DIEN_THOAIs.Single(ctdt => ctdt.ID == id);
            if (chiTietDienThoai != null)
            {
                return ChuyenTuChiTietDienThoaiSangChiTietDienThoaiDTO(chiTietDienThoai);
            }
            return null;
        }

        public static ChiTietDienThoaiDTO LayChiTietDienThoaiTheoMaSanPham(int masanphanm)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            CHI_TIET_DIEN_THOAI chiTietDienThoai = myDB.CHI_TIET_DIEN_THOAIs.Single(ctdt => ctdt.MaSanPham == masanphanm);
            if (chiTietDienThoai != null)
            {
                return ChuyenTuChiTietDienThoaiSangChiTietDienThoaiDTO(chiTietDienThoai);
            }
            return null;
        }

        public static List<string> LayDanhSachCacLoaiManHinh()
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listManHinh = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                              group ctdt by new
                              {
                                  ctdt.KichThuocManHinh
                              };
            List<string> result = new List<string>();
            foreach (var item in listManHinh)
            {
                result.Add(item.Key.KichThuocManHinh);
            }
            return result;
        }

        public static List<string> LayDanhSachCacKieuDang()
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listKieuDang = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                               group ctdt by new
                               {
                                   ctdt.KieuDang
                               };
            List<string> result = new List<string>();
            foreach (var item in listKieuDang)
            {
                result.Add(item.Key.KieuDang);
            }
            return result;
        }

        public static List<ChiTietDienThoaiDTO> TimKiemDienThoaiNangCao(string tenhangsanxuat,
           string kieudang, string kichthuocmanhinh, bool bonhotrong, bool bonhongoai, bool _3G, bool wifi)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            List<ChiTietDienThoaiDTO> result = new List<ChiTietDienThoaiDTO>();

            if (tenhangsanxuat == "Tùy ý")
            {
                #region  Tim theo kieu dang
                if (kieudang == "Tùy ý")
                {
                    #region Tim theo kich thuoc man hinh
                    if (kichthuocmanhinh == "Tùy ý")
                    {
                        #region Tim theo bo nho trong
                        if (bonhotrong == true)
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {

                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {

                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&
                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);


                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {

                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&
                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&
                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }
                                #endregion
                            }
                            #endregion
                        }
                        //Khong ho tro bo nho trong
                        else
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&
                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&
                                                       (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&
                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&
                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);


                                }
                                #endregion
                            }
                            #endregion
                        }
                        #endregion
                    }
                    else
                    {
                        #region Tim theo bo nho trong
                        if (bonhotrong == true)
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&
                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&
                                                       (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&
                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&
                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }
                                #endregion
                            }
                            #endregion
                        }
                        //Khong ho tro bo nho trong
                        else
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&
                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&
                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&
                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&
                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }
                                #endregion
                            }
                            #endregion
                        }
                        #endregion
                    }
                    #endregion
                }
                else
                {
                    #region Tim theo kich thuoc man hinh
                    if (kichthuocmanhinh == "Tùy ý")
                    {
                        #region Tim theo bo nho trong
                        if (bonhotrong == true)
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&
                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&
                                                       (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);


                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&
                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&
                                                       (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }
                                #endregion
                            }
                            #endregion
                        }
                        //Khong ho tro bo nho trong
                        else
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);


                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);


                                }
                                #endregion
                            }
                            #endregion
                        }
                        #endregion
                    }
                    else
                    {
                        #region Tim theo bo nho trong
                        if (bonhotrong == true)
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);


                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);


                                }
                                #endregion
                            }
                            #endregion
                        }
                        //Khong ho tro bo nho trong
                        else
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);


                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    return NewMethod(result, listDienThoai);


                                }
                                #endregion
                            }
                            #endregion
                        }
                        #endregion
                    }
                    #endregion
                }
                #endregion
            }
            else
            {
                HangSanXuatDTO hangsanxuat = HangSanXuatDAO.LayHangSanXuatTheoTenHangSanXuatVaNhomSanPham(tenhangsanxuat,2);
                int mahangsanxuat = hangsanxuat.ID;

                #region  Tim theo kieu dang
                if (kieudang == "Tùy ý")
                {
                    #region Tim theo kich thuoc man hinh
                    if (kichthuocmanhinh == "Tùy ý")
                    {
                        #region Tim theo bo nho trong
                        if (bonhotrong == true)
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;


                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }
                                #endregion
                            }
                            #endregion
                        }
                        //Khong ho tro bo nho trong
                        else
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;


                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;


                                }
                                #endregion
                            }
                            #endregion
                        }
                        #endregion
                    }
                    else
                    {
                        #region Tim theo bo nho trong
                        if (bonhotrong == true)
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;


                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;


                                }
                                #endregion
                            }
                            #endregion
                        }
                        //Khong ho tro bo nho trong
                        else
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;


                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;


                                }
                                #endregion
                            }
                            #endregion
                        }
                        #endregion
                    }
                    #endregion
                }
                else
                {
                    #region Tim theo kich thuoc man hinh
                    if (kichthuocmanhinh == "Tùy ý")
                    {
                        #region Tim theo bo nho trong
                        if (bonhotrong == true)
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&
                                                        ctdt.CacKieuKetNoi.Contains("3G") &&

                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }
                                #endregion
                            }
                            #endregion
                        }
                        //Khong ho tro bo nho trong
                        else
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }
                                #endregion
                            }
                            #endregion
                        }
                        #endregion
                    }
                    else
                    {
                        #region Tim theo bo nho trong
                        if (bonhotrong == true)
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong != "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }
                                #endregion
                            }
                            #endregion
                        }
                        //Khong ho tro bo nho trong
                        else
                        {
                            #region Tim theo bo nho ngoai
                            if (bonhongoai == true)
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro != "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;


                                }
                                #endregion
                            }
                            //Khong ho tro bo nho ngoai
                            else
                            {
                                #region Tim theo 3G
                                if (_3G == true)
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        ctdt.CacKieuKetNoi.Contains("3G") &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }

                                //Khong co 3G
                                else
                                {
                                    var listDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                                        where ctdt.SAN_PHAM.MaHangSanXuat == mahangsanxuat &&
                                                        ctdt.KieuDang == kieudang &&
                                                        ctdt.KichThuocManHinh == kichthuocmanhinh &&
                                                        ctdt.DungLuongBoNhoTrong == "Không" &&
                                                        ctdt.DungLuongBoNhoNgoaiHoTro == "Không" &&

                                                        (ctdt.CacKieuKetNoi.Contains("3G") == false) &&
                                                        (ctdt.CacKieuKetNoi.Contains("Wifi") == wifi)
                                                        select ctdt;
                                    result = NewMethod(result, listDienThoai);
                                    if (result.Count > 0)
                                        return result;

                                }
                                #endregion
                            }
                            #endregion
                        }
                        #endregion
                    }
                    #endregion
                }
                #endregion
            }
            return result;
        }

        private static List<ChiTietDienThoaiDTO> NewMethod(List<ChiTietDienThoaiDTO> result, IQueryable<CHI_TIET_DIEN_THOAI> listDienThoai)
        {
            foreach (CHI_TIET_DIEN_THOAI item in listDienThoai)
            {
                result.Add(ChuyenTuChiTietDienThoaiSangChiTietDienThoaiDTO(item));
            }
            return result;
        }

        public static List<ChiTietDienThoaiDTO> LayDanhSachChiTietDienThoaiTheoNhomDienThoai(int nhomdienthoai)
        {
            List<ChiTietDienThoaiDTO> result = new List<ChiTietDienThoaiDTO>();
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            
            var listChiTietDienThoai = from ctdt in myDB.CHI_TIET_DIEN_THOAIs
                                       where ctdt.NhomDienThoai == nhomdienthoai
                                       select ctdt;

            foreach (CHI_TIET_DIEN_THOAI item in listChiTietDienThoai)
            {
                ChiTietDienThoaiDTO temp = ChuyenTuChiTietDienThoaiSangChiTietDienThoaiDTO(item);
              
                result.Add(temp);
            }
            return result;
        }

        public static bool ThemChiTietDienThoai(ChiTietDienThoaiDTO chitietdienthoai)
        {
            CHI_TIET_DIEN_THOAI ctdt = ChuyenTuChiTietDienThoaiDTOSangChiTietDienThoai(chitietdienthoai);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            try
            {
                myDB.CHI_TIET_DIEN_THOAIs.InsertOnSubmit(ctdt);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        public static bool XoaChiTietDienThoaiTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            CHI_TIET_DIEN_THOAI ctdt = myDB.CHI_TIET_DIEN_THOAIs.Single(c => c.ID == id);
            try
            {
                myDB.CHI_TIET_DIEN_THOAIs.DeleteOnSubmit(ctdt);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;                
            }
            
        }
        
        public static bool XoaChiTietDienThoaiTheoMaSanPham(int masanpham)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            CHI_TIET_DIEN_THOAI ctdt = myDB.CHI_TIET_DIEN_THOAIs.Single(c => c.ID == masanpham);
            try
            {
                myDB.CHI_TIET_DIEN_THOAIs.DeleteOnSubmit(ctdt);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        #endregion

        #region 2   -   Functions Helper

        public static ChiTietDienThoaiDTO ChuyenTuChiTietDienThoaiSangChiTietDienThoaiDTO(CHI_TIET_DIEN_THOAI chitietdienthoai)
        {

            ChiTietDienThoaiDTO result = new ChiTietDienThoaiDTO();
            result.CacKieuKetNoi = chitietdienthoai.CacKieuKetNoi;
            result.Camera = chitietdienthoai.Camera;
            result.DoPhanGiaiManHinh = chitietdienthoai.DoPhanGiaiManHinh;
            result.DungLuongBoNhoNgoaiHoTro = chitietdienthoai.DungLuongBoNhoNgoaiHoTro;
            result.DungLuongBoNhoTrong = chitietdienthoai.DungLuongBoNhoTrong;
            result.ID = chitietdienthoai.ID;
            result.KichThuoc = chitietdienthoai.KichThuoc;
            result.KichThuocManHinh = chitietdienthoai.KichThuocManHinh;
            result.KieuChuong = chitietdienthoai.KieuChuong;
            result.KieuDang = chitietdienthoai.KieuDang;
            result.MaSanPham = chitietdienthoai.MaSanPham.Value;
            result.NhatKyCuocGoi = chitietdienthoai.NhatKiCuocGoi;
            result.NhomDienThoai = chitietdienthoai.NhomDienThoai.Value;
            result.Pin = chitietdienthoai.Pin;
            result.Rung = true;
            result.SoLuongDanhBa = chitietdienthoai.SoLuongDanhBa;
            result.TanSo = chitietdienthoai.TanSo;
            result.ThoiGianCho = chitietdienthoai.ThoiGianCho;
            result.ThoiGianThoai = chitietdienthoai.ThoiGianThoai;
            result.ThongTinBaoHanh = chitietdienthoai.ThongTinBaoHanh;
            result.ThongTinManHinh = chitietdienthoai.ThongTinManHinh;
            result.ThongTinThem = chitietdienthoai.ThongTinThem;
            result.TinNhan = chitietdienthoai.TinNhan;
            result.TrongLuong = chitietdienthoai.TrongLuong;
            return result;
            //return new ChiTietDienThoaiDTO(chiTietDienThoai.ID, chiTietDienThoai.MaSanPham.Value,
            //        chiTietDienThoai.KieuDang, chiTietDienThoai.KichThuoc, chiTietDienThoai.TrongLuong,
            //        chiTietDienThoai.ThongTinManHinh, chiTietDienThoai.KichThuocManHinh,
            //        chiTietDienThoai.DoPhanGiaiManHinh, chiTietDienThoai.KieuChuong, chiTietDienThoai.Rung.Value,
            //        chiTietDienThoai.SoLuongDanhBa, chiTietDienThoai.NhatKiCuocGoi, chiTietDienThoai.DungLuongBoNhoTrong,
            //        chiTietDienThoai.DungLuongBoNhoNgoaiHoTro, chiTietDienThoai.TinNhan, chiTietDienThoai.CacKieuKetNoi,
            //        chiTietDienThoai.Camera, chiTietDienThoai.Pin, chiTietDienThoai.ThoiGianCho,
            //        chiTietDienThoai.ThoiGianThoai, chiTietDienThoai.ThongTinBaoHanh, chiTietDienThoai.NhomDienThoai.Value,
            //        chiTietDienThoai.ThongTinThem);
        }

        public static CHI_TIET_DIEN_THOAI ChuyenTuChiTietDienThoaiDTOSangChiTietDienThoai(ChiTietDienThoaiDTO chitietdienthoaiDTO)
        {
            CHI_TIET_DIEN_THOAI result = new CHI_TIET_DIEN_THOAI();
            result.CacKieuKetNoi = chitietdienthoaiDTO.CacKieuKetNoi;
            result.Camera = chitietdienthoaiDTO.Camera;
            result.DoPhanGiaiManHinh = chitietdienthoaiDTO.DoPhanGiaiManHinh;
            result.DungLuongBoNhoNgoaiHoTro = chitietdienthoaiDTO.DungLuongBoNhoNgoaiHoTro;
            result.DungLuongBoNhoTrong = chitietdienthoaiDTO.DungLuongBoNhoTrong;
            result.ID = chitietdienthoaiDTO.ID;
            result.KichThuoc = chitietdienthoaiDTO.KichThuoc;
            result.KichThuocManHinh = chitietdienthoaiDTO.KichThuocManHinh;
            result.KieuChuong = chitietdienthoaiDTO.KieuChuong;
            result.KieuDang = chitietdienthoaiDTO.KieuDang;
            result.MaSanPham = chitietdienthoaiDTO.MaSanPham;
            result.NhatKiCuocGoi = chitietdienthoaiDTO.NhatKyCuocGoi;
            result.NhomDienThoai = chitietdienthoaiDTO.NhomDienThoai;
            result.Pin = chitietdienthoaiDTO.Pin;
            result.Rung = true;
            result.SoLuongDanhBa = chitietdienthoaiDTO.SoLuongDanhBa;
            result.TanSo = chitietdienthoaiDTO.TanSo;
            result.ThoiGianCho = chitietdienthoaiDTO.ThoiGianCho;
            result.ThoiGianThoai = chitietdienthoaiDTO.ThoiGianThoai;
            result.ThongTinBaoHanh = chitietdienthoaiDTO.ThongTinBaoHanh;
            result.ThongTinManHinh = chitietdienthoaiDTO.ThongTinManHinh;
            result.ThongTinThem = chitietdienthoaiDTO.ThongTinThem;
            result.TinNhan = chitietdienthoaiDTO.TinNhan;
            result.TrongLuong = chitietdienthoaiDTO.TrongLuong;
            return result;
        }

        #endregion
    }
}
